from django.http import request
from django.utils import timezone

from answer.models import Answer
from questions.models import QuestionFocus

DAYS = 30  # 超过30天时间分为0  收藏500 回答250 时间默认3000 每过一天减100 专业相关+3000
INIT_CORE = DAYS * 100


def flag(x):
    score = INIT_CORE
    now = timezone.now()
    if request.user.expertise_area in x.tags:
        score += 3000
    time_range = (now - x.created_time).days
    if time_range >= DAYS:
        score -= INIT_CORE
    else:
        score = score - time_range * 100
    shoucang_score = QuestionFocus.objects.filter(question=x).count() *500
    huida_score = Answer.objects.filter(question=x).count() *250
    return score + shoucang_score + huida_score